package com.winning.pmph.utils;

import lombok.extern.slf4j.Slf4j;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.apache.commons.lang3.StringUtils;

import java.io.File;

/**
 * @author 刘亚飞
 * @Description
 * @create 2020-10-19 11:43
 */
@Slf4j
public class ZipUtil {

    /**
     * 返回值为zip包的临时路径
     * @param srcDir
     * @param password
     * @param zipName
     * @return
     */
    public static void toZip(String srcDir, String password, String zipName) {
        // 设置压缩文件参数
        ZipParameters parameters = new ZipParameters();
        // 设置压缩方法
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        // 设置压缩级别
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
        ZipFile zipFile;
        File zipTempFile = new File(srcDir, zipName + ".zip");
        try {
            if (StringUtils.isNotBlank(password)) {
                parameters.setEncryptFiles(true);
                // 设置aes加密强度
                parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
                // 设置加密方法
                parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
                // 设置密码
                parameters.setPassword(password.toCharArray());
            }
            zipFile = new ZipFile(zipTempFile);
            File srcFile = new File(srcDir);
            //开始压缩
            File[] files = srcFile.listFiles();
            for (File item : files) {
                if (item.isDirectory()) {
                    zipFile.addFolder(item, parameters);
                } else {
                    zipFile.addFile(item, parameters);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("压缩文件时异常");
        }
    }

    public static void deleteDir(String dirPath) {
        File file = new File(dirPath);
        if(file.isFile()) {
            file.delete();
        } else {
            File[] files = file.listFiles();
            if(files == null) {
                file.delete();
            } else {
                for (int i = 0; i < files.length; i++) {
                    deleteDir(files[i].getAbsolutePath());
                }
                file.delete();
            }
        }
    }

    /**
     * 解压缩zip
     *
     * @param zipFilePath
     * @throws ZipException
     */
    public static void unZip(String zipFilePath, String header) throws Exception {
        String filePath = StringUtils.removeEnd(zipFilePath, ".zip");
        File zipFile = new File(zipFilePath);
        ZipFile zFile = new ZipFile(zipFile);
        log.info("客户端的操作系统是 header:{}", header);
        String charset = "GBK";
        if (StringUtils.isNotEmpty(header) && header.contains("Mac")) {
            charset = "UTF-8";
        }
        zFile.setFileNameCharset(charset);
        if (!zFile.isValidZipFile()) {
            throw new Exception("压缩文件不合法,可能已损坏!");
        }
        File destDir = new File(filePath);
        if (destDir.isDirectory() && !destDir.exists()) {
            destDir.mkdir();
        }
        // true 在子线程中进行解压，false主线程中解压
        zFile.setRunInThread(false);
        // 将压缩文件解压到filePath中...
        zFile.extractAll(filePath);
    }
}
